
package com.xinmao.quantitative.trad.strategies.combine;

import com.xinmao.quantitative.trad.strategies.IStrategy;
import org.springframework.stereotype.Component;
import org.ta4j.core.*;
import org.ta4j.core.indicators.SMAIndicator;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.indicators.helpers.UnstableIndicator;
import org.ta4j.core.num.DecimalNum;
import org.ta4j.core.num.Num;
import org.ta4j.core.rules.CrossedDownIndicatorRule;
import org.ta4j.core.rules.CrossedUpIndicatorRule;

@Component
public class UnstableIndicatorStrategy implements IStrategy {

    @Override
    public Strategy buildStrategy(BarSeries series) {
        ClosePriceIndicator close = new ClosePriceIndicator(series);
        int smaPeriod = 3;
        Indicator<Num> sma = new UnstableIndicator(new SMAIndicator(close, smaPeriod), smaPeriod - 1);

        Rule entryRule = new CrossedUpIndicatorRule(close, sma);
        Rule exitRule = new CrossedDownIndicatorRule(close, sma);

        BaseStrategy strategy = new BaseStrategy(entryRule, exitRule);
        strategy.setUnstableBars(3);
        return strategy;
    }

    @Override
    public Num getScore() {
        return DecimalNum.valueOf(1);
    }

}
